package com.datastructure.practice;

public class DeleteReverseNNode {

    public static void main(String[] args) {

    }

    public static ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode pre = new ListNode();
        pre.next=head;
        ListNode pre1 = pre;
        int length = getListNodeLength(pre);
        if(head==null){
            return new ListNode();
        }
        if(n>length){
            return new ListNode();
        }
        for(int i = 0;i<(length-n);i++){
            pre = pre.next;
        }
        pre.next = pre.next.next;
        return pre1.next;
    }

    public static int getListNodeLength(ListNode head){
        int length = 0;
        ListNode pre = head.next;
        if(pre==null){
            return 0;
        }
        while (pre!=null){
            pre=pre.next;
            length++;
        }
        return length;
    }
}

